package extremeconfig;

import extremebeans.AppRuningParas;
import extremebeans.Record;

import beans.MobileMachineInfo;
import beans.UploadData;

import com.mcobject.extremedb.Database;
import com.mcobject.extremedb.DatabaseError;

public class Db {
	static final int PAGE_SIZE = 128;
    static final int DISK_PAGE_SIZE = 4096;
    static final int DISK_CACHE_SIZE = 8*1024*1024;
    static final int DATABASE_SIZE = 256*1024*1024;
    static final String DBIMAGE_FILENAME = "E:\\db.img";
    
    private Database db;
    boolean closeDB = false;

    public Db() {
        int config = 0;
        Database.Device devs[];
        Database.Parameters params = new Database.Parameters();

        // get config parameters from the command line
        String args[]=new String[4];
        for (String s : args) {
            if ("disk".equals(s))  config |= Database.MCO_CFG_DISK_SUPPORT;
            if ("shm".equals(s))   config |= Database.MCO_CFG_SHARED_MEMORY;
            if ("debug".equals(s)) config |= Database.MCO_CFG_DEBUG_LIBRARY;
            if ("mvcc".equals(s))  config |= Database.MCO_CFG_MVCC_TRANSACTION_MANAGER;
        }

        params.memPageSize = PAGE_SIZE; // memory page size
        params.classes = new Class[] { Record.class,AppRuningParas.class,MobileMachineInfo.msgInfo.class }; // list of classes which should be stored in eXremeDB database. The classes must use @Persistent annotation
        params.maxConnections = 10;
        
        if ((config & Database.MCO_CFG_DISK_SUPPORT) != 0) { // on-disk configuration

            params.diskPageSize = DISK_PAGE_SIZE; // disk page size
            params.diskClassesByDefault = true; // mark @Persistent classes as on-disk classes by default

            // declare memory devices
            devs = new Database.Device[4];

            if ((config & Database.MCO_CFG_SHARED_MEMORY) != 0) {
                devs[0] = new Database.SharedMemoryDevice(Database.Device.Kind.Data, "opendb-db", 0, DATABASE_SIZE);
                devs[1] = new Database.SharedMemoryDevice(Database.Device.Kind.DiskCache, "opendb-cache", 0, DISK_CACHE_SIZE);
            } else {
                devs[0] = new Database.PrivateMemoryDevice(Database.Device.Kind.Data, DATABASE_SIZE);
                devs[1] = new Database.PrivateMemoryDevice(Database.Device.Kind.DiskCache, DISK_CACHE_SIZE);
            }
                
            devs[2] = new Database.FileDevice(Database.Device.Kind.Data, "opendb.dbs");
            devs[3] = new Database.FileDevice(Database.Device.Kind.TransactionLog, "opendb.log");

        } else { // in-memory configuration

            devs = new Database.Device[1];

            if ((config & Database.MCO_CFG_SHARED_MEMORY) != 0) {
                devs[0] = new Database.SharedMemoryDevice(Database.Device.Kind.Data, "opendb-db", 0, DATABASE_SIZE);
            } else {
                devs[0] = new Database.PrivateMemoryDevice(Database.Device.Kind.Data, DATABASE_SIZE);
            }
        }

        // create Database object
        db = new Database(config);
        try {
            db.open("opendb", params, devs); // open database. If database already opened DatabaseError exception thrown
            closeDB = true;
            System.out.println("Open " + 
                (((config & Database.MCO_CFG_DISK_SUPPORT) != 0) ? "on-disk" : "in-memory") + " database on " + 
                (((config & Database.MCO_CFG_SHARED_MEMORY) != 0) ? "shared" : "conv") + " memory. Use " + 
                (((config & Database.MCO_CFG_MVCC_TRANSACTION_MANAGER) != 0) ? "MVCC" : "MURSIW") + " transaction manager");
        } catch (DatabaseError dbe) {
            if (dbe.errorCode != 66) { // code 66 means duplicate instance. Valid case for SHM configuration
                throw dbe;
            }
        }
    }
    
    public Database getDb(){
      return db;
    }
     
}
